<?php
namespace Kuoqi\Worker;

/**
 * 命令解析器
 * Class CommParser
 * @package Kuoqi\Worker
 * User kuoqi
 * Datetime 2022/3/24 11:25
 * Remark
 */
class CommParser
{
    /**
     * 命令本身
     * @var string
     * User kuoqi
     * Datetime 2022/3/24 11:28
     * Remark
     */
    protected $comm = '';

    /**
     * @var string
     * User kuoqi
     * Datetime 2022/3/24 11:29
     * Remark 命令行为
     */
    public $action = '';

    /**
     * 路由信息
     * @var array
     * User kuoqi
     * Datetime 2022/3/24 11:34
     * Remark
     */
    public $routes = array();

    /**
     * 初始化方法
     * CommParser constructor.
     * @param $comm
     */
    public function __construct($comm)
    {
        $this->comm = $comm;
    }

    /**
     * 初始化命令信息
     * User kuoqi
     * Datetime 2022/3/24 12:11
     * Remark
     */
    public function init(){
        if(!$this->comm){ return;}
        preg_replace('/\s+/',' ',$this->comm);
        $arr = explode(' ',$this->comm);
        $this->action = $arr[0];
        foreach ($arr as $k=>$it){
            if($k%2){
                switch($it){
                    //绑定路由信息到执行列表
                    case '-r':isset($arr[$k+1])?$this->explainRoute($arr[$k+1]):'';break;
                }
            }
        }
    }

    /**
     * 路由解析
     * @param $stream
     * User kuoqi
     * Datetime 2022/3/24 11:02
     * Remark
     */
    final function explainRoute($stream){
        $this->routes [] = $stream;
    }

    /**
     * 获取行为信息
     * @return string
     * User kuoqi
     * Datetime 2022/3/24 11:29
     * Remark
     */
    public function getAction(){
        return $this->action;
    }
}